﻿using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using cc = Ceyhun.Configuration;
using Ceyhun.WPFGraph;
using System.Windows.Controls;

namespace Ceyhun.MGrammar
{
    public partial class MainWindow : Window
    {
        cc.Configuration config;
        string MgxPath = "";
        string InstanceDocPath = "";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            config = new cc.Configuration();
            MgxPath = config["LastMgx"];
            InstanceDocPath = config["LastInstanceDoc"];
            if (MgxPath != "" && InstanceDocPath != "") {
                Display(MgxPath, InstanceDocPath);
            }
            mnuDisplay.Click += (o, evt) => { Display(MgxPath, InstanceDocPath); };
        }

        private void Display(string MgxPath, string InstanceDocPath)
        {
            if (!FilesOK(MgxPath, InstanceDocPath)) return;

            MGraph mg = new MGraph();
            mg.Load(MgxPath);
            string code = File.ReadAllText(InstanceDocPath);
            mg.ParseString(code);
            Graph g = new Graph();
            g.EdgeLength = 30;

            string rootLabel = mg.Root.Label == null ? mg.Root.ToString() : mg.Root.Label;
            var root = new Ceyhun.WPFGraph.Node(rootLabel + mg.Root.GetHashCode(), rootLabel);
            g.AddNode(root);
            GenGraph(g, root, mg.Root);

            CodeTextBlock.Text = code;
            ScrollViewer viewer = new ScrollViewer()
            {
                HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                Content = g.Layout()
            };
            DisplayGrid.Children.Clear();
            DisplayGrid.Children.Add(viewer);
        }

        private void GenGraph(Graph g, Ceyhun.WPFGraph.Node parentNode, Node parentMGNode)
        {
            foreach (var n in parentMGNode.Children)
            {
                string childLabel = n.Label == null ? n.ToString() : n.Label;
                var child = new Ceyhun.WPFGraph.Node(childLabel + n.GetHashCode(), childLabel);
                g.AddEdge(parentNode, child);
                GenGraph(g, child, n);
            }
        }

        private void menuOpenMg_Click(object sender, RoutedEventArgs e)
        {
            string path = config["LastMgx"];
            string folder = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (path == "")
            {
                path = Assembly.GetExecutingAssembly().Location;
            }
            else
            {
                ofd.FileName = path;
            }
            folder = new FileInfo(path).DirectoryName;
            ofd.InitialDirectory = folder;
            ofd.Filter = "Mgx Files (*.mgx)|*.mgx";
            if (ofd.ShowDialog() == true)
            {
                MgxPath = ofd.FileName;
                config["LastMgx"] = MgxPath;
            }
        }

        private void menuOpenInstance_Click(object sender, RoutedEventArgs e)
        {
            string path = config["LastInstanceDoc"];
            string folder = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (path == "")
            {
                path = Assembly.GetExecutingAssembly().Location;
            }
            else
            {
                ofd.FileName = path;
            }
            folder = new FileInfo(path).DirectoryName;
            ofd.InitialDirectory = folder;
            if (ofd.ShowDialog() == true)
            {
                InstanceDocPath = ofd.FileName;
                config["LastInstanceDoc"] = InstanceDocPath;
            }
        }

        private static bool FilesOK(string MgxPath, string InstanceDocPath)
        {
            if (MgxPath == "")
            {
                MessageBox.Show("MGrammar not selected.");
                return false;
            }

            if (InstanceDocPath == "")
            {
                MessageBox.Show("Instance document not selected.");
                return false;
            }

            return true;
        }

    }
}
